<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="regrtest.html" />
<link rel="prev" href="module-test.html" />
<link rel="parent" href="module-test.html" />
<link rel="next" href="regrtest.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>23.4.1 Writing Unit Tests for the test package</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="23.4 test  "
  href="module-test.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="23.4 test  "
  href="module-test.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="23.4.2 running tests using"
  href="regrtest.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-test.html">23.4 test  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-test.html">23.4 test  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="regrtest.html">23.4.2 Running tests using</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h2><a name="SECTION0025410000000000000000"></a><a name="writing-tests"></a>
<br>
23.4.1 Writing Unit Tests for the <tt class="module">test</tt> package
</h2>

<p>
It is preferred that tests for the <tt class="module">test</tt> package use the
<tt class="module"><a href="module-unittest.html">unittest</a></tt> module and follow a few guidelines.
One is to name the test module by starting it with "<tt class="samp">test_</tt>" and end it with
the name of the module being tested.
The test methods in the test module should start with "<tt class="samp">test_</tt>" and end with
a description of what the method is testing.
This is needed so that the methods are recognized by the test driver as
test methods.
Also, no documentation string for the method should be included.
A comment (such as
"<tt class="samp"># Tests function returns only True or False</tt>") should be used to provide
documentation for test methods.
This is done because documentation strings get printed out if they exist and
thus what test is being run is not stated.

<p>
A basic boilerplate is often used:

<p>
<div class="verbatim"><pre>
import unittest
from test import test_support

class MyTestCase1(unittest.TestCase):

    # Only use setUp() and tearDown() if necessary

    def setUp(self):
        ... code to execute in preparation for tests ...

    def tearDown(self):
        ... code to execute to clean up after tests ...

    def test_feature_one(self):
        # Test feature one.
        ... testing code ...

    def test_feature_two(self):
        # Test feature two.
        ... testing code ...

    ... more test methods ...

class MyTestCase2(unittest.TestCase):
    ... same structure as MyTestCase1 ...

... more test classes ...

def test_main():
    test_support.run_unittest(MyTestCase1,
                              MyTestCase2,
                              ... list other tests ...
                             )

if __name__ == '__main__':
    test_main()
</pre></div>

<p>
This boilerplate code allows the testing suite to be run by
<tt class="module">test.regrtest</tt> as well as on its own as a script.

<p>
The goal for regression testing is to try to break code.
This leads to a few guidelines to be followed:

<p>

<ul>
<li>The testing suite should exercise all classes, functions, and
      constants.
      This includes not just the external API that is to be presented to the
      outside world but also "private" code.
</li>
<li>Whitebox testing (examining the code being tested when the tests are
      being written) is preferred.
      Blackbox testing (testing only the published user interface) is not
      complete enough to make sure all boundary and edge cases are tested.
</li>
<li>Make sure all possible values are tested including invalid ones.
      This makes sure that not only all valid values are acceptable but also
      that improper values are handled correctly.
</li>
<li>Exhaust as many code paths as possible.
      Test where branching occurs and thus tailor input to make sure as many
      different paths through the code are taken.
</li>
<li>Add an explicit test for any bugs discovered for the tested code.
      This will make sure that the error does not crop up again if the code is
      changed in the future.
</li>
<li>Make sure to clean up after your tests (such as close and remove all
      temporary files).
</li>
<li>If a test is dependent on a specific condition of the operating system
      then verify the condition already exists before attempting the test.
</li>
<li>Import as few modules as possible and do it as soon as possible.
      This minimizes external dependencies of tests and also minimizes possible
      anomalous behavior from side-effects of importing a module.
</li>
<li>Try to maximize code reuse.
      On occasion, tests will vary by something as small as what type
      of input is used.
      Minimize code duplication by subclassing a basic test class with a class
      that specifies the input:
<div class="verbatim"><pre>
class TestFuncAcceptsSequences(unittest.TestCase):

    func = mySuperWhammyFunction

    def test_func(self):
        self.func(self.arg)

class AcceptLists(TestFuncAcceptsSequences):
    arg = [1,2,3]

class AcceptStrings(TestFuncAcceptsSequences):
    arg = 'abc'

class AcceptTuples(TestFuncAcceptsSequences):
    arg = (1,2,3)
</pre></div>
</li>
</ul>

<p>
<div class="seealso">
  <p class="heading">See Also:</p>

<dl compact="compact" class="seetitle">
    <dt><em class="citetitle"
        >Test Driven Development</em></dt>
    <dd>A book by Kent Beck on writing tests before code.</dd>
  </dl>
</div>

<p>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="23.4 test  "
  href="module-test.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="23.4 test  "
  href="module-test.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="23.4.2 running tests using"
  href="regrtest.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-test.html">23.4 test  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-test.html">23.4 test  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="regrtest.html">23.4.2 Running tests using</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
